package com.tangcco.lockwell.service.impl;

import com.tangcco.lockwell.domain.AnswerSheet;
import com.tangcco.lockwell.repository.AnswerSheetMapper;
import com.tangcco.lockwell.service.AnswerSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户答题情况
 *
 * @author yanxinping
 * @version 1.0
 * @description: TODO
 * @date 2024/12/18 11:20
 */
@Service
public class AnswerSheetServiceImpl implements AnswerSheetService {

    @Value("${total.week.question}")
    private int weekPercentageQuestion;

    @Value("${total.month.question}")
    private int monthPercentageQuestion;

    @Value("${total.year.question}")
    private int yearPercentageQuestion;

    @Value("${total.week.time}")
    private int weekPercentageTime;

    @Value("${total.month.time}")
    private int monthPercentageTime;

    @Value("${total.year.time}")
    private int yearPercentageTime;

    @Autowired
    private AnswerSheetMapper answerSheetMapper;

    @Override
    public List<AnswerSheet> selectList(AnswerSheet answerSheet) {
        return answerSheetMapper.selectList(answerSheet);
    }

    @Override
    public int insert(AnswerSheet answerSheet) {
        return answerSheetMapper.insertSelective(answerSheet);
    }

    @Override
    public int deleteById(int id) {
        return answerSheetMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<BigDecimal> selectQuestionCompleted(int userId, String time) {
        List<BigDecimal> numcompleteds = new ArrayList<>();
        int numcompleted = 0;
        BigDecimal fortyPercent;
        if (time.equals("week")) {
            fortyPercent = new BigDecimal(weekPercentageQuestion);
        } else if (time.equals("month")) {
            fortyPercent = new BigDecimal(monthPercentageQuestion);
        } else if (time.equals("year")) {
            fortyPercent = new BigDecimal(yearPercentageQuestion);
        } else {
            // 处理未知的时间类型
            fortyPercent = new BigDecimal(100); // 默认值或其他处理逻辑
        }

        // 获取当前日期
        LocalDate now = LocalDate.now();
        LocalDate startDate;
        if (time.equals("week")) {
            startDate = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        } else if (time.equals("month")) {
            startDate = now.with(TemporalAdjusters.firstDayOfMonth());
        } else if (time.equals("year")) {
            startDate = now.with(TemporalAdjusters.firstDayOfYear());
        } else {
            startDate = now; // 默认值或其他处理逻辑
        }

        // 将开始日期与0点组合成 LocalDateTime 对象
        LocalDateTime startDateTime = LocalDateTime.of(startDate, java.time.LocalTime.MIDNIGHT);
        // 使用 DateTimeFormatter 格式化为 SQL 兼容的字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String sqlFormat = startDateTime.format(formatter);
        numcompleted = answerSheetMapper.selectQuestionCompleted(userId, sqlFormat);
        BigDecimal totalque = new BigDecimal(numcompleted);
        BigDecimal percentage = totalque.divide(fortyPercent, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
        // 如果比例超过100%，则设置为100%
        if (percentage.compareTo(new BigDecimal("100")) > 0) {
            percentage = new BigDecimal("100");
        }
        // 将totalHour向下取整
        BigDecimal totalnum = totalque.setScale(0, RoundingMode.DOWN);
        numcompleteds.add(totalnum);
        numcompleteds.add(percentage);

        return numcompleteds;
    }
    @Override
    public List<BigDecimal> selectTotalTime(int userId, String time) {
        Long countTime = 0L;
        List<BigDecimal> totalTimeSpent = new ArrayList<>();
        BigDecimal fortyPercent;
        if (time.equals("week")) {
            fortyPercent = new BigDecimal(weekPercentageTime);
        } else if (time.equals("month")) {
            fortyPercent = new BigDecimal(monthPercentageTime);
        } else if (time.equals("year")) {
            fortyPercent = new BigDecimal(yearPercentageTime);
        } else {
            // 处理未知的时间类型
            fortyPercent = new BigDecimal(40); // 默认值或其他处理逻辑
        }

        // 获取当前日期
        LocalDate now = LocalDate.now();
        LocalDate startDate;
        if (time.equals("week")) {
            startDate = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        } else if (time.equals("month")) {
            startDate = now.with(TemporalAdjusters.firstDayOfMonth());
        } else if (time.equals("year")) {
            startDate = now.with(TemporalAdjusters.firstDayOfYear());
        } else {
            startDate = now; // 默认值或其他处理逻辑
        }

        // 将开始日期与0点组合成 LocalDateTime 对象
        LocalDateTime startDateTime = LocalDateTime.of(startDate, java.time.LocalTime.MIDNIGHT);
        // 使用 DateTimeFormatter 格式化为 SQL 兼容的字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String sqlFormat = startDateTime.format(formatter);

        countTime = answerSheetMapper.selectTotalTime(userId, sqlFormat);
        BigDecimal bigDecimal = new BigDecimal(countTime);
        // 将除数 1000 * 60 * 60 转换为 BigDecimal 类型
        BigDecimal divisor = new BigDecimal(1000).multiply(new BigDecimal(60));
        BigDecimal totalHour = bigDecimal.divide(divisor, 2, RoundingMode.HALF_UP);
        BigDecimal percentage = totalHour.divide(fortyPercent, 4, RoundingMode.HALF_UP).multiply(new BigDecimal("100"));
        // 如果比例超过100%，则设置为100%
        if (percentage.compareTo(new BigDecimal("100")) > 0) {
            percentage = new BigDecimal("100");
        }
        // 将totalHour向下取整
        BigDecimal totalHourFloored = totalHour.setScale(0, RoundingMode.DOWN);
        totalTimeSpent.add(totalHourFloored);
        totalTimeSpent.add(percentage);

        return totalTimeSpent;
    }

}
